package com.dtjd.playcoinmonkey.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.adapter.HomeListAdapter;
import com.dtjd.playcoinmonkey.bean.HomeListBean;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivityMyCollectionBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MyCollectionActivity extends AppCompatActivity {

    private final List<HomeListBean> data=new ArrayList<>();
    private HomeListAdapter adp;
    private int current=1;
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMyCollectionBinding binding = ActivityMyCollectionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(getResources().getString(R.string.mycollections));
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        adp=new HomeListAdapter(this, data, (v, position) -> {

        }, (v, position) -> {

        }, (v, position) -> {
            List<HttpParaBean> para=new ArrayList<>();
            para.add(new HttpParaBean("token","token"));
            para.add(new HttpParaBean("bpId",data.get(position).id));
            Network.post(para, "wbhMain/handleCollect", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            Tools.toast(new JSONObject(Objects.requireNonNull(response.body()).string()).getString("datalist"));
                            refresh(null,null);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
            });
        });
        binding.myCollectionList.setAdapter(adp);
        binding.myCollectRefresh.setRefreshHeader(new ClassicsHeader(this));
        binding.myCollectRefresh.setRefreshFooter(new ClassicsFooter(this));
        binding.myCollectRefresh.setOnRefreshListener(refreshLayout -> {
            current=1;
            refresh(refreshLayout,null);
        });
        binding.myCollectRefresh.setOnLoadMoreListener(refreshLayout -> refresh(null,refreshLayout));
        binding.myCollectionList.setOnItemClickListener((adapterView, view, i, l) -> {
            MyApplication.postBean=data.get(i);
            startActivity(new Intent(MyCollectionActivity.this,PostDetailActivity.class));
        });
        refresh(null,null);
    }
    private void refresh(RefreshLayout refreshLayout, RefreshLayout loadmorelayout) {
        if (loadmorelayout!=null&&refreshLayout==null) current++;
        ArrayList<HttpParaBean> para=new ArrayList<>();
        para.add(new HttpParaBean("token","token"));
        para.add(new HttpParaBean("current",String.valueOf(current)));
        para.add(new HttpParaBean("size","10"));
        Network.post(para, "wbhMain/myCollect", new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        JSONObject jo1=new JSONObject(Objects.requireNonNull(response.body()).string());
                        JSONArray ja = jo1.getJSONArray("datalist");
                        if (loadmorelayout==null) data.clear();
                        if (ja.length()!=0){
                            for (int i = 0; i < ja.length(); i++) {
                                JSONObject jo = ja.getJSONObject(i);
                                JSONArray jo_imgs = jo.getJSONArray("imgs");
                                String img_url="";
                                if (jo_imgs.length()>0) img_url= jo_imgs.getString(0);
                                String bannedCount = "0";
                                if (jo.has("bannedCount")) bannedCount=jo.getString("bannedCount");
                                data.add( new HomeListBean(2,jo.getString("bp_content"), img_url, jo.getString("bp_report"), jo.getString("bp_sort"), jo.getString("bp_title"), jo.getString("bp_type"), jo.getInt("bu_id"), jo.getString("id"), jo.getInt("bp_click_number"), jo.getString("time"), jo.getString("wb_nickname"), jo.getString("wb_url"),bannedCount,null));
                            }
                        }
                        adp.notifyDataSetChanged();
                        if (refreshLayout!=null)refreshLayout.finishRefresh(0);
                        if (loadmorelayout!=null)loadmorelayout.finishLoadMore(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });
    }
}